package com.crm.controller;

import java.util.Date;
import java.util.Map;

import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.crm.entity.Message;
import com.crm.service.MessageService;
import com.crm.utils.DateUtil;
import com.crm.utils.PageUtil;
import com.crm.utils.ResponeMessageUtil;
import com.crm.utils.UUIDUtil;

/**
 * 查询消息
 */
@RestController
@RequestMapping("/message")
public class MessageController {
	
	@Autowired
	private MessageService messageService;
	
	/**
	 * 发送消息
	 * @param message
	 * @return
	 */
	@RequestMapping("/createMessage")
	public boolean createMessage(Message message) {
		message.setId(UUIDUtil.getUUID());
		message.setSendtime(DateUtil.dateToString(new Date()));
		return messageService.insert(message)>0?true:false;
	}
	
	/**
	 * 查看全部消息
	 * @param page
	 * @param limit
	 * @param receiverid
	 * @param sendtime
	 * @param endDate
	 * @return
	 */
	@RequestMapping("/selectMseeageAll")
	public ResponeMessageUtil selectMseeageAll(Integer page, Integer limit, String receiverid ,String sendtime ,String endDate) {
		Integer setValue = PageUtil.getSetValue(page, limit);
		return ResponeMessageUtil.ok(messageService.getCount(receiverid, sendtime, endDate),messageService.selectReceiverIdAll(setValue,limit, receiverid, sendtime, endDate));
	}
	
	/**
	 * 修改消息状态
	 * @param id
	 * @param valid
	 * @param message
	 * @return
	 */
	@RequestMapping("/updateMessageIsRead/{id}/{valid}")
	public boolean updateMessageIsRead(@PathVariable("id")String id,@PathVariable("valid")String valid,Message message) {
		message.setId(id);
		if (valid == "on" || "on".equals(valid)) {
			message.setIsread(1);
		} else {
			message.setIsread(0);
		}
		return messageService.update(message)>0?true:false;
	}
	
	@RequestMapping("/findMessageDanMu")
	public Map<String, Object> findMessageDanMu(String receiverid) {
		Map<String, Object> map = new HashedMap<String, Object>();
		map.put("message", messageService.selectMessage(receiverid)!=null?"xiaoxi":"");
		map.put("data", 0);
		return map;
	}

}
